package com.ly.hotel.spainterface.jg.utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * Created by ThinkPad on 2018/1/24.
 */

public class OkHttpUtils {

    private static final byte[] LOCKER = new byte[0];
    private static OkHttpUtils mInstance;
    private OkHttpClient mHttpClient;

    private OkHttpUtils(){
        mHttpClient = new OkHttpClient();
        mHttpClient.setConnectTimeout(10,TimeUnit.SECONDS);
        mHttpClient.setReadTimeout(20,TimeUnit.SECONDS);
        mHttpClient.setWriteTimeout(10,TimeUnit.SECONDS);
    }

    public static OkHttpUtils getInstance(){
        if(mInstance == null){
            synchronized (LOCKER){
                mInstance = new OkHttpUtils();
            }
        }
        return mInstance;
    }

    // 请求图片资源(同步)
    public InputStream getImageResource(String imageUrl){
        Request request = new Request.Builder().url(imageUrl).build();
        InputStream is = null;
        Call call = mHttpClient.newCall(request);
        try {
            Response response = call.execute();
            if(response.isSuccessful()){
                is = response.body().byteStream();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return is;
    }

    // 请求文本资源(异步)
    public void getTextResource(String url,final Handler handler, final int judgeGoodsNum){
        Request request = new Request.Builder().url(url).build();
        Call call = mHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.d("JgActivity","get请求数据出错！");
                Message message = new Message();
                message.what = 2;
                message.arg2 = judgeGoodsNum;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if(response.isSuccessful()){
                    Log.d("JgActivity","post请求数据成功！");
                    String responseString = response.body().string();
                    Message message = new Message();
                    message.what = 1;
                    message.arg1 = judgeGoodsNum;
                    message.obj = responseString;
                    handler.sendMessage(message);
                }
            }
        });

    }

    // 请求json数据(异步)
    public void postJsonResource(String jsonString, String url, final Handler handler, final int judgeGoodsNum){
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8")
                , jsonString);
        Request request = new Request.Builder().post(requestBody).url(url).build();
        Call call = mHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.d("JgActivity","post请求数据出错！");
                Message message = new Message();
                message.what = 2;
                message.arg2 = judgeGoodsNum;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d("JgActivity","post请求数据成功");
                String responseString = response.body().string();
                Log.d("JgActivity",responseString);
                Message message = new Message();
                message.what = 1;
                message.arg1 = judgeGoodsNum;
                message.obj = responseString;
                handler.sendMessage(message);
            }
        });
    }

}
